define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
            };
        },
        getSurplusAmount:function () {
            var self = this;
            servers.finance.surplusAmount(function(res){
                var newJson= $.ExtCommon.getMappingJson(res.success?res.data:{},{surplusAmount:"surplusAmount"},true);
                res.dataOriginal=res.data;
                res.data=newJson;
                self.set({dataSurplusAmount: res});
                self.trigger('bind:SurplusAmount');
            });
        },
        getSurplusAmountByCustomer:function (id) {
            var self = this;
            servers.finance.surplusAmountByCustomer({advertiserId:id},function(res){
                var newJson= $.ExtCommon.getMappingJson(res.success?res.data:{},{surplusAmount:"surplusAmount"},true);
                res.dataOriginal=res.data;
                res.data=newJson;
                self.set({dataCustomerSurplusAmount: res});
                self.trigger('bind:CustomerSurplusAmount');
            });
        },
        getAgentListTransfer:function (param) {
            var self = this;
            var paramMap=servers.getPageParamMap();
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.finance.agentListTransfer(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo=$.ExtCommon.getMappingJson(info,{
                            id:"id",customerName:"advertiserName",serviceCost:"serviceCost"
                        },true);
                        newInfo.serviceCost=$.ExtCommon.isCheckEmpty(newInfo.serviceCost)?"0":newInfo.serviceCost;
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataAgentListTransfer: res});
                self.trigger('bind:AgentListTransfer');
            });
        },

        saveAgentTransferIn:function (json) {
            var self = this;
            var data=this._getMappingAgentTransfer(json);
            servers.finance.agentTransferIn(data, function (res) {
                self.set({dataSaveAgentTransferIn: {req: json, res: res}});
                self.trigger('save:AgentTransferIn');
            });
        },
        saveAgentTransferOut:function (json) {
            var self = this;
            var data=this._getMappingAgentTransfer(json);
            servers.finance.agentTransferOut(data, function (res) {
                self.set({dataSaveAgentTransferOut: {req: json, res: res}});
                self.trigger('save:AgentTransferOut');
            });
        },
        _getMappingAgentTransfer:function(json){
            return $.ExtCommon.getMappingJson(json, {
                advertiserId: "id",description: "description",amount:"amount"
            });
        },
        getAgentListStream:function (param) {
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{startDate:"startTime",endDate:"endTime"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.finance.agentListStream(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",time:"time",into:"into",out:"out",remark:"Remarks",surplusAmount:"surplusAmount"
                        },true);
                        newInfo.into=$.ExtCommon.isCheckEmpty(newInfo.into,"0")?"-":newInfo.into;
                        newInfo.out=$.ExtCommon.isCheckEmpty(newInfo.out,"0")?"-":newInfo.out;
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataAgentListStream: res});
                self.trigger('bind:AgentListStream');
            });
        },
        getCustomerListTransfer:function (param) {
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{startDate:"startTime",endDate:"endTime"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.finance.customerListTransfer(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",time:"time",into:"into",out:"out",surplus:"surplus",remark:"Remarks"
                        },true);
                        newInfo.into=$.ExtCommon.isCheckEmpty(newInfo.into,"0")?"-":newInfo.into;
                        newInfo.out=$.ExtCommon.isCheckEmpty(newInfo.out,"0")?"-":newInfo.out;
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataCustomerListTransfer: res});
                self.trigger('bind:CustomerListTransfer');
            });
        },
        getCustomerListConsume:function (param) {
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{startDate:"startTime",endDate:"endTime"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.finance.customerListConsume(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        return $.ExtCommon.getMappingJson(info,{
                            id:"id",time:"time",out:"out",surplus:"surplus",remark:"Remarks"
                        },true);
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataCustomerListConsume: res});
                self.trigger('bind:CustomerListConsume');
            });
        }
    });
});